#!/bin/bash
# author: yangdong
# function:监控业务进程内存使用情况，每隔指定时间间隔将业务进程占用内存情况写入
# "/tmp/业务进程名+_mem_info.txt文件中"

procs=(csv-srv streamdrs streamts)
time_interval=30

get_system_arch()
{
    uname -a|grep arm >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "ARM"
    else
        echo "X86"
    fi
}

function show_proc_mem()
{
	arch=`get_system_arch`
	if [ "${arch}" = "ARM"  ]; then
		echo "arch:$arch"
		pids=`ps -ef | grep $1 | grep -v grep | awk '{print $1}'`
	else
	    pids=`ps -ef | grep $1 | grep -v grep | awk '{print $2}'`
	fi

	for pid in ${pids}
	do
		#echo "pid = ${pid}"
		proc_name=`cat /proc/${pid}/status | grep Name | awk '{print $2}'`
        #echo ${proc_name}
		echo "date:$(date)" "pid:${pid}" $(cat /proc/${pid}/status | grep VmRSS) >> /tmp/${proc_name}_mem_info.txt
		
	done
}

while true
do
	for proc in ${procs}
	do
		show_proc_mem ${proc}
	done
	sleep ${time_interval}
done

